package cn.ivicar.app.resource;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.Inject;
import com.google.inject.servlet.RequestScoped;

import cn.com.carsmart.exception.BadRequestException;
import cn.com.carsmart.exception.CarsmartException;
import cn.com.carsmart.exception.InternalErrorException;
import cn.com.carsmart.exception.NotAuthorizedException;
import cn.com.carsmart.exception.util.ExceptionResponseBuilder;
import cn.com.carsmart.ws.util.CacheUtil;
import cn.ivicar.app.controller.DevicesSettingController;
import net.sf.json.JSONObject;

@Path("devices/setting")
@RequestScoped
public class DevicesSettingResource {

	private static transient final Logger LOGGER = LoggerFactory.getLogger(DevicesSettingResource.class);

	@Inject
	private DevicesSettingController devicesSettingController;

	@Path("changeId")
	@GET
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
	@Consumes(MediaType.APPLICATION_JSON + ";charset=utf-8")
	public Response getChangeId() {
		try {
			JSONObject result = devicesSettingController.getChangeId();
			return CacheUtil.noCache(Response.ok(result)).build();
		} catch (Exception e) {
			LOGGER.error("请求最新的设备设置的chargeId出错:", e);
			return ExceptionResponseBuilder.build(new InternalErrorException(e));
		}
	}

	@GET
	@Path("settings")
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
	@Consumes(MediaType.APPLICATION_JSON + ";charset=utf-8")
	public Response getSettings() {
		try {

			JSONObject result = devicesSettingController.getSettings();
			return CacheUtil.noCache(Response.ok(result)).build();
		} catch (Exception e) {
			LOGGER.error("获取最新的设备设置出错:", e);
			return ExceptionResponseBuilder.build(new InternalErrorException(e));
		}
	}
	
	@Path("clear")
	@GET
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
	@Consumes(MediaType.APPLICATION_JSON + ";charset=utf-8")
	public Response clear() {
		try {
			boolean result = devicesSettingController.clearRedis();
			return CacheUtil.noCache(Response.ok(new JSONObject().accumulate("result", result))).build();
		} catch (Exception e) {
			LOGGER.error("清除device param redis cache出错:", e);
			return ExceptionResponseBuilder.build(new InternalErrorException(e));
		}
	}

}
